package com.fjzx.androidframe.base.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Display;
import android.view.View;

import butterknife.ButterKnife;

/**
 * @author xiongyongcong
 */
public abstract class BaseBottomSheetDialogFragment extends BottomSheetDialogFragment {
    protected final String TAG = getClass().getSimpleName();
    protected Activity mActivity;
    protected BottomSheetDialog dialog;
    protected BottomSheetBehavior<View> mBehavior;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (Activity) context;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (dialog == null) {
            dialog = new BottomSheetDialog(mActivity);

            Display display = getActivity().getWindowManager().getDefaultDisplay();
            Point screenSize = new Point();
            display.getSize(screenSize);
            int height = screenSize.y * 2 / 3;

            View contentView = getLayoutInflater().inflate(bindDialogView(), null);
            dialog.setContentView(contentView);
            ButterKnife.bind(this, contentView);

            mBehavior = BottomSheetBehavior.from((View) contentView.getParent());
            contentView.post(() -> mBehavior.setPeekHeight(height));
            initView();
            initData();
        }
        return dialog;
    }

    public void show(FragmentManager manager) {
        super.show(manager, TAG);
    }

    protected void initView() {
    }

    protected abstract int bindDialogView();

    protected abstract void initData();
}
